<?php

class FileModel extends CI_Model{
    
    function getFilesByUserAndGroup($userId, $groupId){
        /*
            SELECT files.fileId, files.name AS name, files.size, files.date
            FROM files, user_file
            WHERE user_file.userId = 3
            AND user_file.groupId = 5
            AND user_file.fileId = files.fileId
         */
        
        $this->db->select ('files.fileId, files.name AS name, files.size, files.date');
        $this->db->from ('files, user_file');
        $this->db->where('user_file.userId',$userId);
        $this->db->where('user_file.groupId',$groupId);
        $this->db->where('user_file.fileId = files.fileId'); 
        $this->db->order_by('files.date');
        $query = $this->db->get();
        return $query->result();
        
        
    }
    
    function addFile($name, $size, $date, $ip){
        /*
        INSERT INTO files
        VALUES (NULL, 'archivo1.zip', '12393432', 02/17/2014, '192.168.1.5')
         */
        $data = array(
               'fileId' => NULL,
               'name' => $name,
               'size' => $size,
               'date' => $date,
               'ip' => $ip
            );

        $this->db->insert('files', $data); 
    }
    
    function addUserFile($userId, $fileId, $groupId){
       /*
        INSERT INTO files
        VALUES (NULL, 'archivo1.zip', '12393432', 02/17/2014, '192.168.1.5')
       */
        $data = array(
               'userFileId' => NULL,
               'userId' => $userId,
               'fileId' => $fileId,
               'groupId' => $groupId
            );

        $this->db->insert('user_file', $data);
    }
    
    function getNextAutoIncrementValue(){
       
        $sql = "SELECT AUTO_INCREMENT 
                    FROM information_schema.TABLES 
                    WHERE TABLE_SCHEMA = 'filecollectordb'
                    AND TABLE_NAME = 'files'";
        $query = $this->db->query($sql);
        $array = $query->result();
        $object = $array[0];
        return $object->AUTO_INCREMENT;
                
    }
    
}
